<?php

class items extends Module
{
	public function index($id)
	{
		$results = $this->db->prepare('SELECT i.id, i.title, i.list, i.ordre, i.category, i.location FROM items i LEFT JOIN lists l ON l.id = i.list WHERE l.owner = ? AND l.id = ? ORDER BY i.ordre',array($this->hash,$id));
		$num_results = count($results);
		
		$this->res->type = 'response';
		
		$this->res->stat = 'OK';
		$this->res->data = array();
		
		if($num_results > 0)
		{
			$this->res->data = $results;
		}
	}
	private function isOwner($list)
	{
		$results = $this->db->prepare('SELECT COUNT(*) num FROM lists WHERE owner = ? AND id = ? LIMIT 1',array($this->hash,$list));
		return ($results[0]['num'] > 0);
	}
	public function create($title,$list,$ordre,$category,$location)
	{
		if($this->isOwner($list))
		{
			$create = $this->db->prepare('INSERT INTO items (title,list,ordre,category,location) VALUES (?,?,?,?,?)',array($title,$list,$ordre,$category,$location),FALSE);

			$result = new StdClass;
			$result->id	  = $this->db->return_id();

			$this->res->type = 'response';		
			$this->res->stat = 'OK';
			$this->res->data = array($result);
		}
		else
		{
			$result = new StdClass;
			$result->message	  = 'You aren\'t the owner of this list!';
			
			$this->res->type = 'response';		
			$this->res->stat = 'FAIL';
			$this->res->data = array($result);
		}
		
	}
	public function update($id,$title,$list,$ordre,$category)
	{
		if($this->isOwner($list))
		{
			$update = $this->db->prepare('UPDATE items SET title = ?,list = ?,ordre = ?,category = ? WHERE id = ?',array($title,$list,$ordre,$category,$id),FALSE);

			$this->res->type = 'response';		
			$this->res->stat = 'OK';
			$this->res->data = array();
		}
		else
		{
			$result = new StdClass;
			$result->message	  = 'You aren\'t the owner of this list!';
			
			$this->res->type = 'response';		
			$this->res->stat = 'FAIL';
			$this->res->data = array($result);
		}
		
	}
	public function delete($id)
	{
		if($this->isOwner($list))
		{
			$delete = $this->db->prepare('DELETE FROM items WHERE id = ?',array($this->hash,$id),FALSE);

			$this->res->type = 'response';		
			$this->res->stat = 'OK';
			$this->res->data = array();
		}
		else
		{
			$result = new StdClass;
			$result->message	  = 'You aren\'t the owner of this list!';
			
			$this->res->type = 'response';		
			$this->res->stat = 'FAIL';
			$this->res->data = array($result);
		}
		
	}
	private function giftExists($id)
	{
		$results = $this->db->prepare('SELECT COUNT(*) num FROM rel_items WHERE item = ? LIMIT 1',array($id));
		return ($results[0]['num'] > 0);		
	}
	public function gift($id=NULL,$money=0,$estrella=0,$type='GIFT')
	{
		if($id === NULL OR !$this->giftExists($id))
		{
			$id = ($id === NULL) ? 'NULL' : '"'.$id.'"';
		
			$create = $this->db->prepare('INSERT INTO rel_items (item,user,money,estrella,type) VALUES ('.$id.',?,?,?,?)',array($this->hash,$money,$estrella,$type),FALSE);
		
			$this->res->type = 'response';		
			$this->res->stat = 'OK';
			$this->res->data = array();
		}
		else
		{
			$result = new StdClass;
			$result->message	  = 'Ya has regalado esto!';
			
			$this->res->type = 'response';		
			$this->res->stat = 'FAIL';
			$this->res->data = array($result);
		}
	}
	public function delete_gift($id)
	{
		$delete = $this->db->prepare('DELETE FROM rel_items WHERE user = ? AND id = ?',array($this->hash,$id),FALSE);

		$this->res->type = 'response';		
		$this->res->stat = 'OK';
		$this->res->data = array();
	}
}


?>